package com.wipz.eoa.iphone.news;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.businessProcess.TaskTransitionService;
import com.wipz.eoa.iphone.util.Constants;
import com.wipz.eoa.iphone.util.DataUtil;
import com.wipz.eoa.iphone.util.DateUtil;
import com.wipz.eoa.iphone.util.UUIDHexGenerator;

/**
 * Title : 公告发布
 * 
 * Discription :公告发布管理
 * 
 * Copyright: 2009-2015 ISoftStone Inc.
 * 
 * Compony:ISoftStone
 * 
 * @author mamingjie
 * @version 1.0
 */
public class NewsPublishAction  extends BaseAction{

	private static final long serialVersionUID = 1L;
	private INewsPublishService newsPublishService;
	private TaskTransitionService taskTransitionService;
	
	public void setTaskTransitionService(TaskTransitionService taskTransitionService) {
		this.taskTransitionService = taskTransitionService;
	}
	/**
	 * 发布公告列表
	 */
	private List list;
	/**
	 * 通知公告信息详情
	 */
	private Map detail;
	
	private Map delete;
	
	public Map getDelete() {
		return delete;
	}

	public void setDelete(Map delete) {
		this.delete = delete;
	}

	/**
	 * 发布公告列表
	 */
	private List publishList;

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Map getDetail() {
		return detail;
	}

	public void setDetail(Map detail) {
		this.detail = detail;
	}

	public void setNewsPublishService(INewsPublishService newsPublishService) {
		this.newsPublishService = newsPublishService;
	}
	/**
	 * 主页面列表查询分页数据
	 * @throws ToftException
	 */
	
	public String list() {
		String qcontent = this.getRequest().getParameter("qcontent");
		try {
			String s = new String(qcontent.getBytes("iso-8859-1"),"utf-8");
			qcontent= java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String commiter = this.getRequest().getParameter("COMMITERCODE");
		Map<String,String>	params = new HashMap<String,String>();
		params.put("", qcontent);
		params.put("COMMITERCODE", commiter);																//定义Map存储上下文对象
		list = newsPublishService.queryList("news.list", params);
		List resultData = new ArrayList();
		for (Object o : list) {
			Map rowData = (Map) o;
			List images = newsPublishService.queryList("upload.queryByFormId", rowData);
			StringBuffer strBuf = new StringBuffer();
			for (int i = 0; i < images.size(); i++) {
				Object obj = images.get(i);
				Map image = (Map) obj;
				String fileName = (String) image.get("FILENAME");
				String appandage = "";
				if (i == 0) {//true?
					appandage = "<a rel='example_group' titl='公文标题' href='"
							+ this.getRequest().getContextPath()
							+ "/upload/" + fileName + "'>浏览附件</a>";
				} else {
					appandage = "<a rel='example_group' titl='公文标题' style='display:none' href='"
							+ this.getRequest().getContextPath()
							+ "/upload/" + fileName + "'></a>";
				}
				strBuf.append(appandage);
			}
			rowData.put("APPENDAGE", strBuf.toString());
			resultData.add(rowData);
		}
		list.add(resultData);
		
		return SUCCESS;
	}
	
	/**
	 * 新增、修改、查看开启子页面
	 * @throws ToftException
	 */
	public String detail() {
		String id = this.getRequest().getParameter("id");;//页面ID值
		String commiter = this.getRequest().getParameter("COMMITERCODE");
		String taskId = this.getRequest().getParameter("taskId");
		if(commiter.isEmpty()) {
			return SUCCESS;
		}
		if (null==id  || id.isEmpty()){  
			//初始化页面上传控件需要的一个唯一标识编号。
			detail.put("COMMITERCODE",commiter);
		//	result.put("CREATEDATE",DateConvertUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss") );
			detail.put("NEWSTYPE", "0");
			detail.put("URGENTTYPE", "0");
			detail.put("add", "true");
			
            String uploadId = UUIDHexGenerator.getInstance().generate();
            detail.put("uploadId", uploadId);
		}else{
			Map<String,String>	params = new HashMap<String,String>();
			params.put("id", id);
			detail = (Map)newsPublishService.queryOne("news.queryById",params);
			//result =this.queryProcessID(result);
			String processInstanceId = (String) detail.get("processInstanceId");
			//this.getContext().addAttribute("processInstanceId", processInstanceId);
			if("2".equals(detail.get("PROCESS_STATUS"))||"1".equals(detail.get("PROCESS_STATUS"))){
				if("0".equals(detail.get("URGENTTYPE"))){
					detail.put("URGENTTYPE", "普通");
				}else{
					detail.put("URGENTTYPE", "紧急");
				}
				if("1".equals(detail.get("NEWSTYPE"))){
					detail.put("NEWSTYPE", "公告");
				}else{
					detail.put("NEWSTYPE", "通知");
				}
			}
			List images = newsPublishService.queryList("upload.queryByFormId", detail);
			StringBuffer strBuf = new StringBuffer();
			for (int i=0;i<images.size();i++){
				Object obj = images.get(i);
				Map image = (Map) obj;
				String fileName = (String) image.get("FILENAME");
				String realName = (String) image.get("REALNAME");
				String appandage = "<a  href='#' onclick='prevewPic(this)' picurl='"+this.getRequest().getContextPath()+"/upload/"+fileName+"'>"+realName+"</a>&nbsp;&nbsp;";
				strBuf.append(appandage);
			}
			detail.put("APPENDAGE", strBuf.toString());
			
			String contents = (String)detail.get("CONTENTS");
			if(contents != null){
				//取得内容中图片路径
				 String img = DataUtil.getImgStr(contents);
				 detail.put("IMG", img);
				 //过滤图片信息
				 contents = DataUtil.filterImg(contents);
				 detail.put("CONTENTS", contents);
			}
			else{
				detail.put("CONTENTS", "");
			}
		}
		
		if(null == taskId || taskId.isEmpty()) {
			return SUCCESS;
		}
		
		detail.put("taskId", taskId);
		List list =(taskTransitionService.getTaskTransition(taskId));
		Map url = new HashMap();
		StringBuffer bf = new StringBuffer();
		bf.append("/news/news_bxSubmit.action?id=");
		bf.append(id);
		bf.append("&COMMITERCODE=");
		bf.append(commiter);
		bf.append("&taskId=");
		bf.append(taskId);
		url.put("url", bf.toString());
		list.add(url);
		detail.put("button", list);
		return SUCCESS;
	}
	
	/**
	 * 新增记录
	 * @throws ToftException
	 */
	public String insert() {
		Map params = new HashMap();
		params = this.getRequest().getParameterMap();
		//首先保存form表单中的信息
		String id = UUIDHexGenerator.getInstance().generate();
		params.put("ID", id);
		params.put("BILLCODE", Constants.getBillCode(Constants.NEWS)); //生成单据CODE
		params.put("PROCESS_STATUS", "0");
		params.put("CREATEDATE",DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss") );
		newsPublishService.insert("news.insert",params );
		return SUCCESS;
	}
	
	/**
	 * 修改记录
	 * @throws ToftException
	 */
	public String update() {
		String id = this.getRequest().getParameter("id");
		if(id==null||id.isEmpty()){
		}else{
			//更新表单
			newsPublishService.insert("news.update", this.getRequest().getParameterMap());
		}
		return SUCCESS;
	}
	
	/**
	 * 逻辑删除
	 * 
	 */
	public String delete() {
		Map params = new HashMap();
		String id = this.getRequest().getParameter("id");
		if(id==null||id.isEmpty()){
		}else{
			params.put("ID", id);
			Map result = (Map)newsPublishService.queryOne("news.check",params);
			delete = new HashMap();
			if(result == null || result.isEmpty()){
				delete.put("result", "1");
			}
			else{
				newsPublishService.delete("news.delete",id);
				delete.put("result", "0");
			}
		}
		return SUCCESS;
	}
	
}
